package com.xilihui.xlh.component.recyclerview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;

public class BaseGridLayoutManager extends GridLayoutManager {

    public BaseGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public BaseGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public BaseGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    public void setSpanSize(BaseAdapter adapter) {
        FooterSpanSizeLookup footerSpanSizeLookup = new FooterSpanSizeLookup(adapter, getSpanCount());
        setSpanSizeLookup(footerSpanSizeLookup);
    }

    private class FooterSpanSizeLookup extends SpanSizeLookup {
        private BaseAdapter adapter;
        private int spanCount;

        private FooterSpanSizeLookup(BaseAdapter adapter, int spanCount) {
            this.adapter = adapter;
            this.spanCount = spanCount;
        }

        @Override
        public int getSpanSize(int position) {

            if (adapter.isTheEnd(position)) {
                // 整行显示
                return spanCount;
            }
            return 1;
        }
    }
}
